package com.ssmeasy.jdgl.serviceImpl;


import com.ssmeasy.jdgl.dao.RoomMapper;
import com.ssmeasy.jdgl.pojo.Room;
import com.ssmeasy.jdgl.pojo.RoomDetail;
import com.ssmeasy.jdgl.service.RoomService;
import com.ssmeasy.jdgl.utils.SStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;


    /**
     * 添加房间
     *
     * @param room
     */
    @Transactional
    @Override
    public void addRoom(Room room) throws Exception {

        //参数判空（id不能为空，价格不能为空，价格必须为指定类型）
        if(room.getId() == null || StringUtils.isEmpty(room.getPrice()) || !SStringUtils.isNumber(room.getPrice())){
           throw new Exception("数字格式不正确");
        }

        roomMapper.insert(room);
    }


    /**
     * 根据状态来获取房间列表
     *
     * @param status
     * @return
     */
    @Override
    public List<Room> getRoomByStatus(int status) {
        return roomMapper.selectByStatus(status);
    }


    /**
     *获取全部房间列表
     *
     * @return
     */
    @Override
    public List<Room> getAllRoom() {
        return roomMapper.selectAll();
    }


    /**
     * 获取房间详细信息
     *
     * @param id
     * @return
     */
    @Override
    public RoomDetail getRoomDetail(int id) {

        return roomMapper.getRoomDetailById(id);


    }

    /**
     * 通过房间id获取房间信息
     *
     * @param id
     * @return
     */

    @Override
    public Room getRoomById(int id) {
        return roomMapper.selectByPrimaryKey(id);
    }


    /**
     * 删除房间
     *
     * @param id
     */
    @Transactional
    @Override
    public void deleteRoom(int id) {
        roomMapper.deleteByPrimaryKey(id);
    }

    /**
     * 更新房间
     *
     * @param room
     */
    @Transactional
    @Override
    public void updateRoom(Room room) throws Exception {

        //参数判空（价格不能为空）
        if (StringUtils.isEmpty(room.getPrice()) || !SStringUtils.isNumber(room.getPrice())) {
            throw new Exception("价格不正确");
        }

        roomMapper.updateByPrimaryKey(room);
    }
}
